<?php 
App::uses('Component', 'Controller');
class ResizeComponent extends Component {
    public function resize($fileName, $max_width, $max_height, $quality, $crop, $folder) {
		require_once (APP.'Vendor'.DS .'phpthumb/phpthumb.class.php');
		
   		$phpThumb = new phpThumb();
		$phpThumb->config_imagemagick_path = '/usr/bin/convert';
		$phpThumb->config_prefer_imagemagick = true;
		$phpThumb->config_output_format = 'png';
		$phpThumb->config_error_die_on_error = true;
		$phpThumb->config_temp_directory = APP.'tmp';
		$phpThumb->config_cache_disable_warning = true;

		$phpThumb->src = FULL_BASE_URL.'/img/uploads/'.$fileName;
		//$phpThumb->src = IMAGES . $fileName;
		CakeLog::write('debug', 'source from ' . $phpThumb->src);		
		// TODO: generate thumbnail						
		$phpThumb->config_cache_directory = IMAGES.$folder.DS;
		$phpThumb->w = $max_width;
		$phpThumb->h = $max_height;
		$phpThumb->q = $quality;
		$phpThumb->zc = $crop;
		
		$phpThumb->cache_filename = $phpThumb->config_cache_directory . $fileName;
		if(!is_file($phpThumb->cache_filename)){
			if($phpThumb->GenerateThumbnail()){
				$phpThumb->RenderToFile($phpThumb->cache_filename);
				$url1 = FULL_BASE_URL.'/img/thumbs/'.$fileName;
				CakeLog::write('debug', 'thumb to ' . $url1);
			}
			else {
				$url1 = FULL_BASE_URL.'/img/uploads/'.$fileName;
				CakeLog::write('debug', 'full failed ' . $url1);
			}
		}
		return $url1;
    }
}